package org.example.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.pojo.CommonResult;
import org.example.pojo.Payment;
import org.example.service.PaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
public class PaymentController {
    @Autowired
    PaymentService paymentService;

    @Value("${server.port}")
    public String port;

    @PostMapping("/payment/create")
    public CommonResult<Payment> create(@RequestBody Payment payment){
        int result =  paymentService.create(payment);
        if(result > 0){
            //新增成功
            return new CommonResult(200,"添加成功",result);
        }else{
            return new CommonResult(500,"添加失败",null);
        }
    }

    /**
     * 根据ID查询订单
     */
    @GetMapping("/payment/{id}")
    public CommonResult<Payment> findPaymentById(@PathVariable("id") Long id){
        Payment payment =  paymentService.findPaymentById(id);
        if(payment !=  null){
            //查询成功
            return new CommonResult(200,"查询成功了,端口号是："+port,payment);
        }else{
            return new CommonResult(500,"查询失败",null);
        }
    }

}
// localhost:8002/payment/1
// localhost:8002/payment/create